package love.yuanmengrobot.attendancesystem.dao;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/20 16:37
 */

import love.yuanmengrobot.attendancesystem.dao.SQL.SqlString;
import love.yuanmengrobot.attendancesystem.entity.Member;
import love.yuanmengrobot.attendancesystem.util.JdbcUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class MemberDao {
    /**
     * 通过ID查询单条数据
     *
     * @param id loginid
     * @return 实例对象
     */
   public Member queryById(String id,String paw,String perm) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(SqlString.selectMember(id,paw,perm));
            if (rs.next()) {
                Member member = new Member(
                        rs.getString("memberid"),
                        rs.getString("loginid"),
                        rs.getString("password"),
                        rs.getString("permission"),
                        rs.getString("name"),
                        rs.getString("sex"),
                        rs.getString("phone"),
                        rs.getString("qq"));
                return member;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }
        return null;
    }

    public Member queryById(String memberid) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(SqlString.selectMember(memberid));
            if (rs.next()) {
                Member member = new Member(
                        rs.getString("memberid"),
                        rs.getString("loginid"),
                        rs.getString("password"),
                        rs.getString("permission"),
                        rs.getString("name"),
                        rs.getString("sex"),
                        rs.getString("phone"),
                        rs.getString("qq"));
                return member;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }
        return null;
    }

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    List<Member> queryAllByLimit(int offset, int limit) {
        return null;
    }

    ;


    /**
     * 通过实体作为筛选条件查询
     *
     * @param member 实例对象
     * @return 对象列表
     */
    List<Member> queryAll(Member member) {
        return null;
    }

    ;

    /**
     * 新增教师
     *
     * @param id 账号 paw 密码
     * @return 影响行数
     */
    public int insertTeacher(String id, String paw, String name) {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            int s = stmt.executeUpdate(SqlString.insertTeacherMember(id, paw,name));
            if (s == 1) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(stmt, conn);
        }

        return 0;
    }

    ;

    /**
     * 新增学生
     *
     * @param id 账号 paw 密码
     * @return 影响行数
     */
    public int insertStudent(String id, String paw,String  name) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            int s = stmt.executeUpdate(SqlString.insertStudentMember(id, paw,name));
            if (s == 1) {
                return 1;
            } else {
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }

        return 0;
    }

    ;

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param entities List<Member> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(List<Member> entities) {
        return 0;
    }

    ;

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<Member> 实例对象列表
     * @return 影响行数
     */
    int insertOrUpdateBatch(List<Member> entities) {
        return 0;
    }

    ;

    /**
     * 修改数据
     *
     * @param member 实例对象
     * @return 影响行数
     */
    int update(Member member) {
        return 0;
    }

    ;

    /**
     * 通过主键删除数据
     *
     * @param memberid 主键
     * @return 影响行数
     */
    int deleteById(Integer memberid) {
        return 0;
    }

    ;

}
